package cn.cvs.servlet;

import cn.cvs.pojo.Fruit;
import cn.cvs.service.FruitService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/FruitUpdateServlet")
public class FruitUpdateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        super.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        FruitService fruit = new FruitService();
        Fruit f = new Fruit();
        f.setFruitId(Integer.parseInt(request.getParameter("fruitId")));
        f.setType(request.getParameter("type"));
        f.setBreed(request.getParameter("breed"));
        f.setArea(request.getParameter("area"));
        f.setBrief(request.getParameter("brief"));
        f.setWeight(Integer.parseInt(request.getParameter("weight")));
        f.setPrice(Float.parseFloat(request.getParameter("price")));

        int count = fruit.update(f);
        if (count>0){
            request.setAttribute("info","修改成功！");
        }else {
            request.setAttribute("info","修改失败！");
        }
        request.getRequestDispatcher("FruitListServlet").forward(request,response);
    }
}
